#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    CPU = new cpu(ui->textEdit);

    CPU->test_registers();
    CPU->test_processor();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_actionOpenBinary_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Files (*.*)"));

    QFile file(fileName);
    if (!file.open(QFile::ReadOnly)) {
        QMessageBox::warning(this, tr("SDI"),
                             tr("Cannot read file %1:\n%2.")
                             .arg(fileName)
                             .arg(file.errorString()));
        return;
    }

    ui->hexeditor->setData(file.readAll());

    CPU->load_program(fileName.toAscii());
    CPU->memoryDump(0, 16);

}

void MainWindow::on_actionOpen_Z80_Code_triggered()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),"",tr("Files (*.*)"));

    QFile file(fileName);
    if (!file.open(QFile::ReadOnly)) {
        QMessageBox::warning(this, tr("SDI"),
                             tr("Cannot read file %1:\n%2.")
                             .arg(fileName)
                             .arg(file.errorString()));
        return;
    }

    ui->textCode->setText(file.readAll());
}

void MainWindow::on_actionRun_triggered()
{
    CPU->run_loaded_program();
}
